package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * SpringAMQP-入门案例
 *
 */
@Component
public class SpringRabbitListener {

    //基本消息队列-第一步：监听simple.queue队列，处理消息
    /*@RabbitListener(queues = "simple.queue")
    public void reSimpleQueue(String msg){
        System.out.println("simple.queue................ = " + msg);
    }*/

    /**
     * 工作队列 监听消息-消费者1 每秒处理40条
     * 多个消费者共同处理消息处理
     */
    @RabbitListener(queues = "simple.queue")
    public void reWorkQueue1(String msg) throws InterruptedException {
        System.out.println("work.queue11111................ = " + msg);
        Thread.sleep(25);
    }

    /**
     * 工作队列 监听消息-消费者2 每秒处理10条
     */
    @RabbitListener(queues = "simple.queue")
    public void reWorkQueue2(String msg) throws InterruptedException {
        System.err.println("work.queue22222................ = " + msg);
        Thread.sleep(100);
    }


    /**
     * 发布订阅-广播模式 生产者将消息投递到交换机，如果交换机绑定了多个队列，那多个队列会收到消息，消费者订阅了队列也会收到消息进行后续业务处理
     */
    @RabbitListener(queues = "fanout.queue1")
    public void reFanoutQueue1(String msg) throws InterruptedException {
        System.err.println("fantou.queue11111................ = " + msg);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void reFanoutQueue2(String msg) throws InterruptedException {
        System.err.println("fantou.queue222222................ = " + msg);
    }

    /**
     * 发布订阅-路由模式  生产者将消息投递到交换机 并指定路由key, 交换机根据路由key 跟队列上的绑定key进行对比，如果一样投递消息到队列中，消费者订阅了队列也会收到消息进行后续业务处理
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "itcast.queue1"),
                    exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
                    key = {"red","blue"}
            )
    )
    public void reDirectQueue1(String msg) throws InterruptedException {
        System.err.println("direct.queue11111................ = " + msg);
    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "itcast.queue2"),
                    exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
                    key = {"red","yellow"}
            )
    )
    public void reDirectQueue2(String msg) throws InterruptedException {
        System.err.println("direct.queue22222................ = " + msg);
    }


    /**
     * 发布订阅-主题模式  生产者将消息投递到交换机 并指定路由key, 交换机根据路由key 跟队列上的绑定key进行对比(# *)，如果规则能够匹配上则投递消息到队列中，消费者订阅了队列也会收到消息进行后续业务处理
     * @param msg
     * @throws InterruptedException
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "topic.queue1"),
                    exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
                    key = "china.#"
            )
    )
    public void reTopicQueue1(String msg) throws InterruptedException {
        System.err.println("topic.queue11111................ = " + msg);
    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "topic.queue2"),
                    exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
                    key = "#.news"
            )
    )
    public void reTopicQueue2(String msg) throws InterruptedException {
        System.err.println("topic.queue22222................ = " + msg);
    }

    /**
     * 消息转换器-对象消息
     */
    @RabbitListener(queues = "object.queue")
    public void reObjectQueue(Map<String,Object> map) {
        System.out.println("object.queue................ = " + map.get("name"));
        System.out.println("object.queue................ = " + map.get("age"));
    }
}
